<?php

namespace Modules\SmallApi\Http\Controllers;

use Illuminate\Http\Request;
use Modules\SmallApi\Service\OrderMallService;
use Modules\Api\Service\MallOrderService;

class OrderMallController extends BaseController
{
    /**
     * 结算台--预生成积分订单
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function preBuildOrderMall(Request $request)
    {
        $arr_input = $request->all();
        $arr_input['user_id'] = small_api_user_id(); //用户id

        $res = MallOrderService::pre_build_order((array)$arr_input);

        return response_json($res);
    }

    /**
     * 创建商品订单
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function createGoodsOrder(Request $request)
    {
        $data = $request->all();
        $data['user_id'] = small_api_user_id(); //用户id

        shiwu_start();
        $res = MallOrderService::create_mall_order($data);
        shiwu_end($res);

        return response_json($res);
    }

    /**
     * 支付订单
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function payGoodsOrder(Request $request)
    {
        $data = $request->all();
        $data['user_id'] = small_api_user_id(); //用户id

        shiwu_start();
        $res = MallOrderService::pay_mall_order((array)$data);
        shiwu_end($res);

        return response_json($res);
    }

    /**
     * 用户获取商城订单列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getUserGoodsOrderList(Request $request)
    {
        $arr_input = $request->all();
        $arr_input['user_id'] = small_api_user_id(); //用户id

        $res = MallOrderService::get_mall_order_list((array)$arr_input);

        return response_json($res);
    }

    /**
     * 用户获取商城订单详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getUserGoodsOrderDetail(Request $request)
    {
        $arr_input = $request->all();

        $res = MallOrderService::get_mall_order_info((array)$arr_input);

        return response_json($res);
    }

    /**
     * 用户确认收货
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function checkDeliveryStatus(Request $request)
    {
        $arr_input = $request->all();
        $arr_input['user_id'] = small_api_user_id(); //用户id

        $res = MallOrderService::mall_order_take_over((array)$arr_input);

        return response_json($res);
    }


    /**
     * 获取指定的订单信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function get_goods_order(Request $request)
    {
        $nId = (int)$request->get('id', 0);
        $res = OrderMallService::get_goods_order((int)$nId);
        return response_json($res);
    }


    /**
     * 检测用户是否有当前商品的未支付订单
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function checked_order_one(Request $request)
    {

        $arr_search = $request->all();
        $res = OrderMallService::checked_order_one((array)$arr_search);
        return response_json($res);
    }


    /**
     * 用户评论订单(积分、人民币商品订单评论)
     * @param Request $request
     * @return array|\Illuminate\Http\JsonResponse
     */
    public function create_order_comment(Request $request)
    {
        try {
            $arr_input = (array)$request->all();
            shiwu_start();
            $res = OrderMallService::create_order_comment((array)$arr_input);
            shiwu_end($res);
            return response_json($res);
        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }


}